package com.yds.views;

import android.content.Context;
import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;

import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

@SuppressWarnings("all")
public class QMUIDialogHelper {
    private static int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    private static QMUITipDialog tipDialog;

    /**
     * 展示文字类型对话框 - 默认按钮文字
     *
     * @param activity
     * @param clickCallBack
     * @param title
     * @param message
     */
    public static void showMessageDialog(FragmentActivity activity, OnDialogClickCallBack clickCallBack,
                                         String title, String message) {
        showMessageDialog(activity, clickCallBack, title, message, "取消", "确定");
    }

    /**
     * 展示文字类型对话框 - 默认按钮文字颜色-蓝色
     *
     * @param activity
     * @param clickCallBack
     * @param title
     * @param message
     * @param leftBtTxt
     * @param rightBtTxt
     */
    public static void showMessageDialog(FragmentActivity activity, OnDialogClickCallBack clickCallBack,
                                         String title, String message, String leftBtTxt, String rightBtTxt) {
        showMessageDialog(activity, clickCallBack, title, message, leftBtTxt, rightBtTxt, false);
    }

    /**
     * 展示文字类型对话框
     *
     * @param activity
     * @param clickCallBack
     * @param title
     * @param message
     * @param leftBtTxt
     * @param rightBtTxt
     * @param isBtRedColor
     */
    public static void showMessageDialog(FragmentActivity activity, OnDialogClickCallBack clickCallBack,
                                         String title, String message, String leftBtTxt, String rightBtTxt, boolean isBtRedColor) {
        new QMUIDialog.MessageDialogBuilder(activity)
                .setTitle(title)
                .setMessage(message)
                .setSkinManager(QMUISkinManager.defaultInstance(activity))
                .addAction(leftBtTxt, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        clickCallBack.onCancel();
                        dialog.dismiss();
                    }
                })
                .addAction(0, rightBtTxt, isBtRedColor ? QMUIDialogAction.ACTION_PROP_NEGATIVE : QMUIDialogAction.ACTION_PROP_POSITIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        clickCallBack.onConfirm();
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    public static class Builder {
        /**
         * 显示 Loading 图标
         */
        public static final int ICON_TYPE_LOADING = 1;
        /**
         * 显示成功图标
         */
        public static final int ICON_TYPE_SUCCESS = 2;
        /**
         * 显示失败图标
         */
        public static final int ICON_TYPE_FAIL = 3;
        /**
         * 显示请勿重复操作图标
         */
        public static final int ICON_TYPE_REPEAT = 4;
    }

    /**
     * 展示提示对话框-默认提示文字
     *
     * @param activity
     * @param icon_type
     */
    public static void showTipsDialog(FragmentActivity activity, int icon_type) {
        showTipsDialog(activity, icon_type, "");
    }

    /**
     * 展示提示对话框
     *
     * @param activity FragmentActivity
     * @param icon_type
     * @param tipsMsg
     */
    public static void showTipsDialog(FragmentActivity activity, int icon_type, String tipsMsg) {
        showTipsDialog(activity, icon_type, tipsMsg);
    }

    /**
     * 展示提示对话框
     *
     * @param context Context
     * @param icon_type
     * @param tipsMsg
     */
    public static void showTipsDialog(Context context, int icon_type, String tipsMsg) {
        if (icon_type == QMUITipDialog.Builder.ICON_TYPE_LOADING) {
            if (TextUtils.isEmpty(tipsMsg)) {
                tipsMsg = "正在加载";
            }
        } else if (icon_type == QMUITipDialog.Builder.ICON_TYPE_SUCCESS) {
            if (TextUtils.isEmpty(tipsMsg)) {
                tipsMsg = "发送成功";
            }
        } else if (icon_type == QMUITipDialog.Builder.ICON_TYPE_FAIL) {
            if (TextUtils.isEmpty(tipsMsg)) {
                tipsMsg = "发送失败";
            }
        } else if (icon_type == QMUITipDialog.Builder.ICON_TYPE_INFO) {
            if (TextUtils.isEmpty(tipsMsg)) {
                tipsMsg = "请勿重复操作";
            }
        }

        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(icon_type)
                .setTipWord(tipsMsg)
                .create();
        tipDialog.show();
    }

    /**
     * 关闭提示对话框
     */
    public static void dismissTipsDialog() {
        if (null != tipDialog) {
            tipDialog.dismiss();
        }
    }

    /**
     * 对话框点击回调
     */
    public interface OnDialogClickCallBack {
        void onConfirm();

        void onCancel();
    }

}
